package com.flycent.fanbao.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.flycent.fanbao.R;
import com.flycent.fanbao.adapter.AbsViewHolderAdapter;
import com.flycent.fanbao.bean.ClassModels;
import com.flycent.fanbao.service.NetworkStateService;
import com.flycent.fanbao.ui.view.SimpleTitleBar;

public class MainActivity extends BackableTitleBarActivity {
	@ViewInject(id = R.id.main_list ) ListView mList;

	List<String> className = new ArrayList<>();

	private List<ClassModels> mData = Arrays.asList(
			//			new ClassModels("笑话", JokeActivity.class), 
			//			new ClassModels("数据", FictionActivity.class), 
			new ClassModels("View集合", ViewSampleMainActivity.class), 
			new ClassModels("你问我答", AskActivity.class), 
			new ClassModels("快递查询", ExpressInfoActivity.class), 
			new ClassModels("私人定制", PrivateCustomActivity.class), 
			new ClassModels("用List实现Grid效果", ListofGridActivity.class), 
			new ClassModels("GridView添加Head", HearderGridActivity.class), 
			new ClassModels("定位", LocationManageActivity.class), 
			new ClassModels("翻译", TranslationActivity.class),
			new ClassModels("volley用法集合", VolleyMainActivity.class));

	private MainAdapter mAdaper;


	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_main);
		FinalActivity.initInjectedView(this);
		getTitleBar().setTitle("首页");


		mAdaper = new MainAdapter(this, R.layout.item_main_list);
		mList.setAdapter(mAdaper);

		mAdaper.update(mData);

		mList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				Intent intent = new Intent(MainActivity.this,mData.get(position).getName());
				startActivity(intent);
			}

		});

		initNetworkService();

	}


	private void initNetworkService(){
		Intent i = new Intent(this,NetworkStateService.class); 
		startService(i);  
	}


	@Override
	protected void onTitleBarCreated(SimpleTitleBar titleBar) {
		super.onTitleBarCreated(titleBar);
		titleBar.hideLeftButton();
	}

	public class MainAdapter extends AbsViewHolderAdapter<ClassModels>{

		public MainAdapter(Context context, int layoutRes) {
			super(context, layoutRes);
		}

		@Override
		protected void bindData(int pos, ClassModels itemData) {
			TextView mTextView = getViewFromHolder(R.id.item_main_text);
			mTextView.setText(""+itemData.getClassTitle());

		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Intent i = new Intent(this,NetworkStateService.class); 
		stopService(i); 
	}


}
